package com.sxkiler.demo.config;

import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

@Component
public class RocketMqProducerConfig {
    private static Logger logger = LoggerFactory.getLogger(RocketMqProducerConfig.class);

    @Value("${rocketmq.namesrvAddr:172.20.178.59:9876}")
    private String namesrvAddr;

    @Value("${rocketmq.producerGroupName:rocketmq}")
    private String producerGroupName;

    @Value("${rocketmq.transactionProducerGroupName:rocketmq}")
    private String transactionProducerGroupName;

    @Value("${rocketmq.vipChannelEnable:false}")
    private String vipChannelEnable;

    //发送的超时时间
    @Value("${rocketmq.sendMsgTimeout:6000}")
    private int sendMsgTimeout;

    /**
     * 创建普通消息发送者实例
     *
     * @return
     * @throws MQClientException
     */
    @Bean("defaultMQProducer")
    public DefaultMQProducer defaultMQProducer() throws MQClientException {
        DefaultMQProducer producer = new DefaultMQProducer(producerGroupName);
        producer.setNamesrvAddr(namesrvAddr);
        producer.setSendMsgTimeout(sendMsgTimeout);
        producer.setVipChannelEnabled(vipChannelEnable != null ? Boolean.valueOf(vipChannelEnable) : false);
        producer.start();
        logger.debug("rocketmq producer server is starting....");
        return producer;
    }

}
